<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mstatisticdownapp extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 
     * @void Country
     * 
     */
    
    public function getStatisticdownappDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';

        $sql = "SELECT created FROM log_downapp WHERE 1=1 GROUP BY `created` ";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND ( `device_name` like '%%".$key."%%' OR ";
            $sql .= " `os_name` like '%%".$key."%%') ";
        }
        
        $sql .= "ORDER BY created DESC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
		foreach ( $xs AS $el=>$le ){
			
			$count_iphone = $this->db->query('SELECT * FROM log_downapp WHERE created = "'.$le->created.'" AND device_name = "iPhone" ')->num_rows();
			$count_android = $this->db->query('SELECT * FROM log_downapp WHERE created = "'.$le->created.'" AND device_name != "iPhone" ')->num_rows();
			$count_total = (int)$count_iphone + (int)$count_android;
			$xs[$el]->count_iphone = $count_iphone;
			$xs[$el]->count_android = $count_android;
			$xs[$el]->count_total = $count_total;
		}
        $count = $this->db->query('SELECT created FROM log_downapp WHERE 1=1')->num_rows();
		$count_iphone = $this->db->query('SELECT created FROM log_downapp WHERE device_name = "iPhone" ')->num_rows();
		$count_android = $count - $count_iphone;
        return array('data' => $xs, 'count' => $count, 'count_iphone' => $count_iphone, 'count_android' => $count_android);
    }
    
}
